package com.elder.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.elder.business.domain.ElderlyInfo;
import com.elder.business.domain.OrderInfo;
import com.elder.business.domain.SuddenRecord;
import com.elder.business.domain.SuddenRecordDTO;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.business.mapper.SuddenRecordMapper;
import com.elder.business.service.ISuddenRecordService;
import com.elder.common.core.domain.R;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.LongUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author elder
 * @date 2024-03-20
 */
@Service
public class SuddenRecordServiceImpl implements ISuddenRecordService
{
    @Autowired
    private SuddenRecordMapper suddenRecordMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param suddenId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SuddenRecordDTO selectSuddenRecordBySuddenId(Long suddenId)
    {
        SuddenRecord v = suddenRecordMapper.selectSuddenRecordBySuddenId(suddenId);
        if(Objects.isNull(v)){
            return new SuddenRecordDTO();
        }
        ElderlyInfo elder = elderlyInfoMapper.getElderInfoByElderId(v.getElderId());
        SuddenRecordDTO dto = new SuddenRecordDTO();
        BeanUtils.copyProperties(v, dto);
        dto.setSuddenId(StringNewUtils.formatStr(v.getSuddenId()));
        dto.setElderId(StringNewUtils.formatStr(v.getElderId()));
        dto.setSuddenTime(DateUtils.dateStrMIAO(v.getSuddenTime()));
        dto.setProcessedEndTime(DateUtils.dateStrMIAO(v.getProcessedEndTime()));
        dto.setElderName(StringNewUtils.formatStr(elder.getElderName()));
        return dto;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @return 【请填写功能名称】
     */
    @Override
    public TableDataInfo selectSuddenRecordList(String elderName, String startTime, String endTime , Integer pageNum, Integer pageSize)
    {
        List<Long> elderIds = new ArrayList<>();
        if(!StringNewUtils.isEmpty(elderName)){
            List<ElderlyInfo> elderinfoList = elderlyInfoMapper.getElderinfoList(elderName);
            elderIds = elderinfoList.stream().map(ElderlyInfo::getElderId).collect(Collectors.toList());
        }
        Long start = null;
        Long end = null;
        if(!StringNewUtils.isEmpty(startTime) && !StringNewUtils.isEmpty(endTime)){
            Long seconds = DateUtils.getDateSeconds(startTime);
            Long dateSeconds = DateUtils.getDateSeconds(endTime);
            start = DateUtils.getDayStartTime(seconds);
            end = DateUtils.getDayEndTime(dateSeconds);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<SuddenRecord> list = suddenRecordMapper.selectSuddenRecordList(elderIds,start,end);
        PageInfo<SuddenRecord> pageInfo = new PageInfo<>(list);
        List<SuddenRecord> recordList = pageInfo.getList();

        List<Long> elderIdList = recordList.stream().map(SuddenRecord::getElderId).collect(Collectors.toList());
        //获取老人信息
        List<ElderlyInfo> elderlyInfos = elderlyInfoMapper.getElderInfoByElderIds(elderIdList);
        Map<Long, String> elderMap = elderlyInfos.stream().collect(Collectors.toMap(ElderlyInfo::getElderId, ElderlyInfo::getElderName));
        List<SuddenRecordDTO> recordDTOList = recordList.stream().map(v -> {
            SuddenRecordDTO dto = new SuddenRecordDTO();
            BeanUtils.copyProperties(v, dto);
            dto.setSuddenId(StringNewUtils.formatStr(v.getSuddenId()));
            dto.setElderId(StringNewUtils.formatStr(v.getElderId()));
            dto.setSuddenTime(DateUtils.dateStrMIAO(v.getSuddenTime()));
            dto.setProcessedEndTime(DateUtils.dateStrMIAO(v.getProcessedEndTime()));
            dto.setElderName(StringNewUtils.formatStr(elderMap.get(v.getElderId())));
            return dto;
        }).collect(Collectors.toList());
        TableDataInfo data = new TableDataInfo();
        data.setCode(200);
        data.setTotal(pageInfo.getTotal());
        data.setRows(recordDTOList);
        return data;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param suddenRecordDTO 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSuddenRecord(SuddenRecordDTO suddenRecordDTO) {
        SuddenRecord dto = new SuddenRecord();
        BeanUtils.copyProperties(suddenRecordDTO, dto);
        dto.setSuddenId(SnowGenerator.getId());
        dto.setElderId(LongUtils.str2Long((suddenRecordDTO.getElderId())));
        dto.setSuddenTime(DateUtils.getDateSeconds(suddenRecordDTO.getSuddenTime()));
        dto.setProcessedEndTime(DateUtils.getDateSeconds(suddenRecordDTO.getProcessedEndTime()));
        return suddenRecordMapper.insertSuddenRecord(dto);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param suddenRecordDTO 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSuddenRecord(SuddenRecordDTO suddenRecordDTO)
    {
        SuddenRecord dto = new SuddenRecord();
        BeanUtils.copyProperties(suddenRecordDTO, dto);
        dto.setElderId(LongUtils.str2Long((suddenRecordDTO.getElderId())));
        dto.setSuddenTime(DateUtils.getDateSeconds(suddenRecordDTO.getSuddenTime()));
        dto.setProcessedEndTime(DateUtils.getDateSeconds(suddenRecordDTO.getProcessedEndTime()));
        return suddenRecordMapper.updateSuddenRecord(dto);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param suddenIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSuddenRecordBySuddenIds(Long[] suddenIds)
    {
        return suddenRecordMapper.deleteSuddenRecordBySuddenIds(suddenIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param suddenId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSuddenRecordBySuddenId(Long suddenId)
    {
        return suddenRecordMapper.deleteSuddenRecordBySuddenId(suddenId);
    }
}
